package com.yun.controller;

import com.yun.dao.db.DbDoctor;
import com.yun.dao.db.DbSickroom;
import com.yun.pojo.Pet;
import com.yun.pojo.User;
import com.yun.pojo.db.Doctor;
import com.yun.pojo.db.Sickroom;
import com.yun.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


@Controller
public class PetController {

    @Autowired
    PetService petService;
    @Autowired
    DbSickroom dbSickroom;
    @Autowired
    DbDoctor dbDoctor;

    @RequestMapping("/getPets")
    public String petInfo(Model model){
        List<Pet> pets = petService.queryPets();
        model.addAttribute("pets",pets);
        return "pet/petInfo";
    }
    @RequestMapping("/addPetPage")
    public String addPetPage(Model model){
        Collection<Sickroom> sickroom = dbSickroom.getSickroom();
        Collection<Doctor> doctor = dbDoctor.getDoctor();
        model.addAttribute("sickrooms",sickroom);
        model.addAttribute("doctors",doctor);
        return "pet/addPet";
    }
    @RequestMapping("/savePet")
    public String savePet(Pet pet,Model model){
        petService.savePet(pet);
        List<Pet> pets = petService.queryPets();
        model.addAttribute("pets",pets);
        return "pet/petInfo";
    }

    @RequestMapping("/updatePetPage/{id}")
    public String updatePetPage(@PathVariable("id")int id, Model model) {
        Pet pet = petService.queryPetById(id);
        Collection<Sickroom> sickroom = dbSickroom.getSickroom();
        Collection<Doctor> doctor = dbDoctor.getDoctor();
        model.addAttribute("sickrooms",sickroom);
        model.addAttribute("doctors",doctor);
        model.addAttribute("pet",pet);
        return "pet/updatePet";
    }


    @RequestMapping("/updatePet")
    public String updatePet(Pet pet,Model model){
        petService.updatePet(pet);
        List<Pet> pets = petService.queryPets();
        model.addAttribute("pets",pets);
        return "pet/petInfo";
    }
    @RequestMapping("/deletePet/{id}")
    public String deletePet(@PathVariable("id")int id,Model model){
        petService.deleteById(id);
        List<Pet> pets = petService.queryPets();
        model.addAttribute("pets",pets);
        return "pet/petInfo";
    }

    @RequestMapping("/queryPerByName")
    public String queryPerByName(String name,Model model){
        List<Pet> list = petService.queryPetByName(name);
        if (list.isEmpty()){

            model.addAttribute("msg","该宠物不存在");
        }
        model.addAttribute("pets",list);
        return "pet/petInfo";
    }
}
